package cn.dckeji.gameBox.service.impl;

import cn.dckeji.gameBox.dto.UserStatis;
import cn.dckeji.gameBox.entity.Customer;
import cn.dckeji.gameBox.dao.CustomerDao;
import cn.dckeji.gameBox.service.CustomerService;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2018-08-20
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, Customer> implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Override
    public List<UserStatis> getStatis(Map<String, Object> maps) {
        return customerDao.getStatis(maps);
    }

    /**
     * 活跃用户数统计
     * @return
     */
    @Override
    public Page<Map<String, Object>>  getUserByDay(int pageNums,int current) {

        Page<Map<String, Object>> page = new Page<>(pageNums,current);
        List<Map<String,Object>> addUserMapList=this.baseMapper.addUserByDay(page);
        List<Map<String,Object>>  loginUserMapList=this.baseMapper.loginUserByDay(page);
        List<Map<String,Object>> tempList=null;
        for(int i=0;i<addUserMapList.size();i++){
            Map<String,Object> addUserMap=addUserMapList.get(i);
            addUserMap.put("loginCount",0);
            tempList  =new ArrayList<>();
            tempList.addAll(loginUserMapList);
            for(int j=0;j<tempList.size();j++){
                Map<String,Object> loginUserMap=tempList.get(j);
                String loginTime=addUserMap.get("createTime").toString().substring(0,10);
                String createTime=loginUserMap.get("loginTime").toString().substring(0,10);
                if(loginTime.equals(createTime)){
                    addUserMap.put("loginCount",loginUserMap.get("count"));
                    loginUserMapList.remove(j);
                    break;
                }
            }
        }
        for( Map<String,Object> loginUserMap:loginUserMapList){
            Map<String,Object> addUserMap=new HashMap<>();
            addUserMap.put("loginTime",loginUserMap.get("loginTime"));
            addUserMap.put("loginCount",loginUserMap.get("count"));
            addUserMap.put("count",0);
            addUserMapList.add(addUserMap);
        }
        return page.setRecords(addUserMapList);
    }
}
